package com.tool.engine.export.mertics.system;

import lombok.Data;

import java.util.Objects;
import java.util.function.UnaryOperator;

@Data
public class MemorySize {

    private long maxMemorySize;

    private long minMemorySize;

    private long usedMemorySize;

    public MemorySize(long maxMemorySize, long minMemorySize, long usedMemorySize) {
        this.maxMemorySize = maxMemorySize;
        this.minMemorySize = minMemorySize;
        this.usedMemorySize = usedMemorySize;
    }

    public MemorySize() {
        this.maxMemorySize = 0;
        this.minMemorySize = Long.MAX_VALUE;
        this.usedMemorySize = 0;
    }

    public UnaryOperator<MemorySize> createFunction(long bytesSize) {
        return old->{
            if(Objects.isNull(old)){
                old = new MemorySize();
            }
            long total = old.getUsedMemorySize() + bytesSize;
            long max = Math.max(old.getMaxMemorySize(), Math.abs(bytesSize));
            long min = Math.min(old.getMinMemorySize(), Math.abs(bytesSize));
           return new MemorySize(max, min, total);
        };

    }
}
